<?php

Class cartController Extends baseController {

	public function index() { 
		
	}
	
	public function viewcart() { 
		if(isset($_SESSION['cart'])){
			$cart = $_SESSION['cart'];
			$this->registry->template->cart = $cart;	
		}
		$this->registry->template->show('cart');
	}
	
	public function viewcart_complete() { 
		
		$this->registry->template->show('cart_complete');
	}
	
	public function deletecart() { 
		$tivi_id = $_GET['tivi_id'];
		if(isset($_SESSION['cart'][$tivi_id])){
			unset($_SESSION['cart'][$tivi_id]);
			
		}
		header('location:gio-hang.html');
		exit;
	}
	
	public function updatecart() { 
		foreach($_POST["qty"] as $tivi_id=>$soluong){
		if($soluong == 0 || $soluong == ""){
			unset($_SESSION["cart"][$tivi_id]);
		}
		else{
			$_SESSION["cart"][$tivi_id] = $soluong;
		}
	}
		header('location:gio-hang.html');
		exit;
	}
	
	public function addcart() {
		 
		$tivi_id = $_GET['tivi_id'];
		//neu da co san pham vua chon trong gio hang
		if(isset($_SESSION['cart'][$tivi_id])):
			$qty = $_SESSION['cart'][$tivi_id] + 1;
			
			echo 'abc';
		//neu chua co san pham vua chon trong gio hang
		else : 
			$qty = 1;
			
		endif;
		$_SESSION['cart'][$tivi_id] = $qty;
		header('location:gio-hang.html');
		exit;
	}
	
	public function insert_order(){
		if(!isset($_SESSION['user'])){
			header('location:gio-hang.html');
			exit;
		}
		//echo '<pre>'; var_dump($_SESSION['cart']); die;
		$hoadon = new hoadon();
		$hoadon->MaHoaDon = $hoadon->setMaHoaDon();
		$hoadon->EmailKH = $_POST['UserName'];	
		$hoadon->TenKH = $_POST['FullName'];
		$hoadon->DienThoaiKH = $_POST['Phone'];
		$hoadon->DiaChiKH = $_POST['Message'];
		$payment = $_POST['PaymentMethod'];
		$hoadon->idPhuongThucThanhToan = str_replace('method','',$payment);
		$hoadon->NgayDatHang = date('Y-m-d');
		$hoadon->TrangThai = 0;
		$hoadon->TenDangNhap = $_SESSION['user']->TenDangNhap;
		
		//echo $email.', '.$fullname.', '.$phone.', '.$address.', '.$payment.', '.$now;	
		
		$insert = $hoadon->insertHoaDon($hoadon);
		if($insert){
			$maxid = $hoadon->getMaxHoaDonId();
			$cart = $_SESSION['cart'];	//echo '<pre>'; var_dump($cart); die;
			foreach($cart as $tivi_id=>$soluong){
				//echo $tivi_id;
				$tiviModel = new tivi();
				$tivi = $tiviModel->getTiviById($tivi_id);	
				$hoadon->insertChiTietHoaDon($maxid,$tivi_id,$tivi->GiaBan,$soluong);
			}
			unset($_SESSION['cart']);
		}
		header('location:gio-hang.html/complete');
		exit;
	}
	
	public function order_history()
	{
		if(!isset($_SESSION['user']))
		{
			header('location:trang-chu.html');	
			exit;
		}
		$hoadonModel = new hoadon();
		$user = $_SESSION['user'];
		$this->registry->template->lstorder = $hoadonModel->getHoaDonByWhere('TenDangNhap = "'.$user->TenDangNhap.'"','','MaHoaDon desc');
		$this->registry->template->show('order_history');
	}
	
	public function order_detail(){
		if(!isset($_SESSION['user']))
		{
			header('location:trang-chu.html');	
			exit;
		}
		$hoadon_id = $_REQUEST['order_id'];
		$hoadonModel = new hoadon();
		$hoadon = $hoadonModel->getHoaDonById($hoadon_id);
		$chitiethd = $hoadonModel->getChiTietHoaDonByIdHoaDon($hoadon_id);
		$this->registry->template->hoadon = $hoadon;
		$this->registry->template->chitiethd = $chitiethd;
		$this->registry->template->show('order_history_detail');
	}
	
	public function update_order(){
		$hoadon_id = $_REQUEST['hoadon_id'];	
		$hoadonModel = new hoadon();
		$rs = $hoadonModel->updateHoaDon('TrangThai = 3',$hoadon_id);
		if($rs){
			header('location:'.__URL_BASE.'lich-su-mua-hang.html');
			exit;	
		}
	}

}

?>
